//
//  TYSystemInfo.m
//  TYBase_Example
//
//  Created by 谢航宇 on 2023/6/28.
//  Copyright © 2023 谢航宇. All rights reserved.
//

#import "TYSystemInfo.h"
#import <sys/utsname.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
@implementation TYSystemInfo


+ (NSString *)getCurrentDeviceName {
    //需要导入头文件：#import <sys/utsname.h>
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString * platform = [NSString stringWithFormat:@"%@",[NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding]];
    NSDictionary * platformDic = @{
        @"iPhone1,1" : @"iPhone 2G",
        
        @"iPhone1,2" : @"iPhone 3G",
        
        @"iPhone2,1" : @"iPhone 3GS",
        
        @"iPhone3,1" : @"iPhone 4",
        @"iPhone3,2" : @"iPhone 4",
        @"iPhone3,3" : @"iPhone 4",
        
        @"iPhone4,1" : @"iPhone 4S",
        
        @"iPhone5,1" : @"iPhone 5",
        @"iPhone5,2" : @"iPhone 5",
        
        @"iPhone5,3" : @"iPhone 5c",
        @"iPhone5,4" : @"iPhone 5c",
        
        @"iPhone6,1" : @"iPhone 5s",
        @"iPhone6,2" : @"iPhone 5s",
        
        @"iPhone7,1" : @"iPhone 6 Plus",
        
        @"iPhone7,2" : @"iPhone 6",
        
        @"iPhone8,1" : @"iPhone 6s",
        
        @"iPhone8,2" : @"iPhone 6s Plus",
        
        @"iPhone8,4" : @"iPhone SE",
        
        @"iPhone9,1" : @"iPhone 7",
        
        @"iPhone9,2" : @"iPhone 7 Plus",
        
        @"iPhone10,1" : @"iPhone 8",
        @"iPhone10,4" : @"iPhone 8",
        
        @"iPhone10,2" : @"iPhone 8 Plus",
        @"iPhone10,5" : @"iPhone 8 Plus",
        
        @"iPhone10,3" : @"iPhone X",
        @"iPhone10,6" : @"iPhone X",
        
        @"iPhone11,2" : @"iPhone XS",
        
        @"iPhone11,4" : @"iPhone XS Max",
        @"iPhone11,6" : @"iPhone XS Max",
        
        @"iPhone11,8" : @"iPhone XR",
        
        @"iPhone12,1" : @"iPhone 11",
        
        @"iPhone12,3" : @"iPhone 11 Pro",
        
        @"iPhone12,5" : @"iPhone 11 Pro Max",
        
        @"iPhone12,8" : @"iPhone SE 2",
        
        @"iPhone13,1" : @"iPhone 12 mini",
        
        @"iPhone13,2" : @"iPhone 12",
        
        @"iPhone13,3" : @"iPhone 12 Pro",
        
        @"iPhone13,4" : @"iPhone 12 Pro Max",
        
        @"iPhone14,2" : @"iPhone 13 Pro",
        
        @"iPhone14,3" : @"iPhone 13 Pro Max",
        
        @"iPhone14,4" : @"iPhone 13 mini",
        
        @"iPhone14,5" : @"iPhone 13",
        
        @"iPhone14,6" : @"iPhone SE 3",
        
        @"iPhone14,7" : @"iPhone 14",
        
        @"iPhone14,8" : @"iPhone 14 Plus",
        
        @"iPhone15,2" : @"iPhone 14 Pro",
        
        @"iPhone15,3" : @"iPhone 14 Pro Max",
        
        
        //iPod
        @"iPod1,1" : @"iPod Touch 1G",
        
        @"iPod2,1" : @"iPod Touch 2G",
        
        @"iPod3,1" : @"iPod Touch 3G",
        
        @"iPod4,1" : @"iPod Touch 4G",
        
        @"iPod5,1" : @"iPod Touch 5G",
        
        @"iPod7,1" : @"iPod Touch 6G",
        
        @"iPod9,1" : @"iPod Touch 7G",
        
        
        //iPad
        @"iPad1,1" : @"iPad 1G",
        
        @"iPad2,1" : @"iPad 2",
        @"iPad2,2" : @"iPad 2",
        @"iPad2,3" : @"iPad 2",
        @"iPad2,4" : @"iPad 2",
        
        @"iPad2,5" : @"iPad Mini 1G",
        @"iPad2,6" : @"iPad Mini 1G",
        @"iPad2,7" : @"iPad Mini 1G",
        
        @"iPad3,1" : @"iPad 3",
        @"iPad3,2" : @"iPad 3",
        @"iPad3,3" : @"iPad 3",
        
        @"iPad3,4" : @"iPad 4",
        @"iPad3,5" : @"iPad 4",
        @"iPad3,6" : @"iPad 4",
        
        @"iPad4,1" : @"iPad Air",
        @"iPad4,2" : @"iPad Air",
        @"iPad4,3" : @"iPad Air",
        
        @"iPad4,4" : @"iPad Mini 2G",
        @"iPad4,5" : @"iPad Mini 2G",
        @"iPad4,6" : @"iPad Mini 2G",
        
        @"iPad4,7" : @"iPad Mini 3",
        @"iPad4,8" : @"iPad Mini 3",
        @"iPad4,9" : @"iPad Mini 3",
        
        @"iPad5,1" : @"iPad Mini 4",
        @"iPad5,2" : @"iPad Mini 4",
        
        @"iPad5,3" : @"iPad Air 2",
        @"iPad5,4" : @"iPad Air 2",
        
        
        @"iPad6,3" : @"iPad Pro 9.7",
        @"iPad6,4" : @"iPad Pro 9.7",
        
        @"iPad6,7" : @"iPad Pro 12.9",
        @"iPad6,8" : @"iPad Pro 12.9",
        
        @"iPad6,11" : @"iPad 5",
        @"iPad6,12" : @"iPad 5",
        
        @"iPad7,1" : @"iPad Pro 12.9 2",
        @"iPad7,2" : @"iPad Pro 12.9 2",
        
        @"iPad7,3" : @"iPad Pro 10.5",
        @"iPad7,4" : @"iPad Pro 10.5",
        
        @"iPad7,5" : @"iPad 6",
        @"iPad7,6" : @"iPad 6",
        
        @"iPad7,11" : @"iPad 7",
        @"iPad7,12" : @"iPad 7",
        
        @"iPad8,1" : @"iPad Pro 11",
        @"iPad8,2" : @"iPad Pro 11",
        @"iPad8,3" : @"iPad Pro 11",
        @"iPad8,4" : @"iPad Pro 11",
        
        @"iPad8,5" : @"iPad Pro 12.9 3",
        @"iPad8,6" : @"iPad Pro 12.9 3",
        @"iPad8,7" : @"iPad Pro 12.9 3",
        @"iPad8,8" : @"iPad Pro 12.9 3",
        
        @"iPad8,9" : @"iPad Pro 11 2",
        @"iPad8,10" : @"iPad Pro 11 2",
        
        @"iPad8,11" : @"iPad Pro 12.9 4",
        @"iPad8,12" : @"iPad Pro 12.9 4",
        
        @"iPad11,1" : @"iPad mini 5",
        @"iPad11,2" : @"iPad mini 5",
        
        @"iPad11,3" : @"iPad Air 3",
        @"iPad11,4" : @"iPad Air 3",
        
        @"iPad11,6" : @"iPad 8",
        @"iPad11,7" : @"iPad 8",
        
        @"iPad12,1" : @"iPad 9",
        @"iPad12,2" : @"iPad 9",
        
        @"iPad13,1" : @"iPad Air 4",
        @"iPad13,2" : @"iPad Air 4",
        
        @"iPad13,4" : @"iPad Pro 11 3",
        @"iPad13,5" : @"iPad Pro 11 3",
        @"iPad13,6" : @"iPad Pro 11 3",
        @"iPad13,7" : @"iPad Pro 11 3",
        
        @"iPad13,8" : @"iPad Pro 12.9 5",
        @"iPad13,9" : @"iPad Pro 12.9 5",
        @"iPad13,10" : @"iPad Pro 12.9 5",
        @"iPad13,11" : @"iPad Pro 12.9 5",
        
        @"iPad13,16" : @"iPad Air 5",
        @"iPad13,17" : @"iPad Air 5",
        
        @"iPad13,18" : @"iPad 10",
        @"iPad13,19" : @"iPad 10",
        
        @"iPad14,1" : @"iPad Air 6",
        @"iPad14,2" : @"iPad Air 6",
    };
    return platformDic[platform]?:platform;
}


//获取手机ip地址
+ (NSString *)getIPAddress {
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    // Free memory
    freeifaddrs(interfaces);
    return address;
}
@end
